﻿using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace Diary.WebAPI.Controllers
{
    [ApiController]
    [Route("[controller]")]
    public class api : ControllerBase
    {
        [HttpGet,HttpPost]
        public string _(string biztype, string function)
        {
            string ret = "";
            switch (biztype)
            {
                case "category":
                    ret = category(function);
                    break;
                case "content":
                    ret = content(function);
                    break;
            }

            return ret;
        }

        string category(string function)
        {
            string ret = "";
            switch (function)
            {
                case "get_all":
                    {
                        List<Model.Category> categories = BAL.Category.query_all();
                        ret = Newtonsoft.Json.JsonConvert.SerializeObject(categories);
                    }
                    break;
                case "insert":
                    {
                        string content = Request.getPostData();
                        Model.Category category = Newtonsoft.Json.JsonConvert.DeserializeObject<Model.Category>(content);
                        if (category != null)
                        {
                            if (BAL.Category.Insert(category) != null)
                            {
                                ret = "ok";
                            }
                        }
                    }
                    break;
                case "update":
                    {
                        string content = Request.getPostData();
                        Model.Category category = Newtonsoft.Json.JsonConvert.DeserializeObject<Model.Category>(content);
                        if (category != null)
                        {
                            if (BAL.Category.Modify(category))
                            {
                                ret = "ok";
                            }
                        }
                    }
                    break;
                case "delete":
                    {
                        int id = Request.getQueryInt("id");
                        if (id > 0)
                        {
                            if (BAL.Category.Delete(id))
                            {
                                ret = "ok";
                            }
                        }
                    }
                    break;
            }

            return ret;
        }

        string content(string function)
        {
            string ret = "";
            switch (function)
            {
                case "get_by_category":
                    {
                        int cid = Request.getQueryInt("cid");
                        List<Model.Content> contents = BAL.Content.query_by_categoryId(cid);
                        ret = Newtonsoft.Json.JsonConvert.SerializeObject(contents);
                    }
                    break;
                case "get_by_id":
                    {
                        int id = Request.getQueryInt("id");
                        Model.Content content = BAL.Content.query_by_id(id);
                        ret = Newtonsoft.Json.JsonConvert.SerializeObject(content);
                    }
                    break;
                case "insert":
                    {
                        string content = Request.getPostData();
                        Model.Content model = Newtonsoft.Json.JsonConvert.DeserializeObject<Model.Content>(content);
                        if (model != null)
                        {
                            if (BAL.Content.Insert(model) != null)
                            {
                                ret = "ok";
                            }
                        }
                    }
                    break;
                case "update":
                    {
                        string content = Request.getPostData();
                        Model.Content category = Newtonsoft.Json.JsonConvert.DeserializeObject<Model.Content>(content);
                        if (category != null)
                        {
                            if (BAL.Content.Modify(category))
                            {
                                ret = "ok";
                            }
                        }
                    }
                    break;
                case "delete":
                    {
                        int id = Request.getQueryInt("id");
                        if (id > 0)
                        {
                            if (BAL.Content.Delete(id))
                            {
                                ret = "ok";
                            }
                        }
                    }
                    break;
            }

            return ret;
        }
    }
}
